﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LeetcodeTest._100._20
{
    public class Leetcode14
    {
        public void Test()
        {
            try
            {
                LongestCommonPrefix(new string[] { });
            }
            catch (Exception ex)
            { }
        }

        /*
         14. Longest Common Prefix 

        Write a function to find the longest common prefix string amongst an array of strings. 
         */

        public string LongestCommonPrefix(string[] strs)
        {
            string strResult = "";
            int minLength = 0;
            bool isFirst = true;
            foreach (var str in strs)
            {
                if (isFirst)
                {
                    minLength = str.Length;
                    isFirst = false;
                    continue;
                }

                if (str.Length < minLength)
                    minLength = str.Length;
            }

            char tempChar = ' ';
            for (int i = 0; i < minLength; i++)
            {
                isFirst = true;
                foreach (var str in strs)
                {
                    if (isFirst)
                    {
                        tempChar = str[i];
                        isFirst = false;
                        continue;
                    }
                    if (str[i] != tempChar)
                        return strResult;
                }
                strResult += tempChar;
            }
            return strResult;
        }
    }
}
